<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_pembobotan extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$kd_kurikulum = isset($_POST['kd_kurikulum']) ? strval($_POST['kd_kurikulum']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_matakuliah, a.nama_matakuliah, c.kd_pembobotan, c.bobot_nilai_tugas, c.bobot_nilai_uts, c.bobot_nilai_uas, c.bobot_presensi, c.bobot_praktikum', false);
			$this->db->from('ref_matakuliah a');
			$this->db->join('ref_kurikulum b', 'b.kd_kurikulum = a.kd_kurikulum');
			$this->db->join('tbl_pembobotan c', 'c.kd_matakuliah = a.kd_matakuliah');
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('a.kd_prodi', $kd_prodi);
			//if($id_tahunajaran != '')
				$this->db->where('b.kd_kurikulum ', $kd_kurikulum);
			
			//$this->db->order_by('a.JamMulai', 'asc');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			//$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function Update_Bobot($data){
		$this->db->trans_start();
		
		$result = true;
		
		# tambah sap ke tabel
		foreach($data['data_pembobotan']['rows'] as $row){
			
			$this->db->flush_cache();
			$this->db->set('bobot_nilai_tugas', $row['bobot_nilai_tugas']);
			$this->db->set('bobot_nilai_uts', $row['bobot_nilai_uts']);
			$this->db->set('bobot_nilai_uas', $row['bobot_nilai_uas']);
			$this->db->set('bobot_presensi', $row['bobot_presensi']);
			$this->db->set('bobot_praktikum', $row['bobot_praktikum']);

			$this->db->where('kd_pembobotan', $row['kd_pembobotan']);

			$result = $this->db->update('tbl_pembobotan');
			
		}
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function getMatakuliah($kd_matakuliah){
		$this->db->flush_cache();
		$this->db->select('nama_matakuliah, sks');
		$this->db->from('ref_matakuliah');
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$res = $this->db->get();
		
		return $res;
	}
	
	function DeleteOnDb($id){
		//query delete data		
		$this->db->flush_cache();
		$this->db->set('status', 'D');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_jadwal', $id);
		
		$result = $this->db->update('tbl_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	//untuk PDF
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.presensi,c.nilai_final,c.nilai_huruf,d.kd_kelas,d.sks', false);
			$this->db->from('tbl_krs_detail c');
			$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('b.kd_prodi', $fil['kd_prodi']);
			//if($id_tahunajaran != '')
				$this->db->where('d.kd_matakuliah ', $fil['kd_matakuliah']);
				$this->db->where('d.kd_dosen ', $fil['kd_dosen']);
				$this->db->where('d.kd_kelas ', $fil['kd_kelas']);
				
				$this->db->where('c.status', 'A');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nim', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama', 'width'=>100, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'SKS', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'Semester', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nilai', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'IPS', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nim,
				$row->nama,
				$row->sks,
				$row->nilai_huruf
			));
			$i++;
		}
		
		return $pdfdata;
		
	}
	
	function getNameByKodeMK($fil){
		$this->db->flush_cache();
		$this->db->from('ref_matakuliah');
		$this->db->order_by('nama_matakuliah');
		$this->db->where('kd_matakuliah', $fil);
		
		$res = $this->db->get()->row();
		
		return $res->nama_matakuliah;
	}
	
	function getNameByKoDosen($fil){
		$this->db->flush_cache();
		$this->db->from('ref_dosen');
		$this->db->order_by('nama_dosen');
		$this->db->where('kd_dosen', $fil);
		
		$res = $this->db->get()->row();
		
		return $res->nama_dosen;
	}
	
}

?>